package com.cheboohui.iqs.deal;

import java.io.BufferedReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.config.PrivateTool;
import com.cheboohui.iqs.controller.BaseController;
import com.cheboohui.iqs.sql.entity.SqlResult;
import com.cheboohui.iqs.taiping.back.BackYeGaunDeal;

public class BackDeal {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(BackDeal.class);
	
	public static void back(HttpServletResponse response,int type,JSONObject tpJson,SqlResult dbResult){
		if(1==type){
			log.info("支付回调接口....");
			BackYeGaunDeal.payBack(tpJson, dbResult);
		}else if(2==type){
			log.info("承保回调接口...");
			BackYeGaunDeal.contractBack(tpJson, dbResult);
		}else{
			log.info("其他回调接口请求.........");
		}
		
		BaseController.setResponseText(response, tpBack());
	}

	/**
	 * 回调处理方法
	 * 
	 * @param request
	 * @param response
	 * @param type
	 *            0统一回调;1支付回调;2承保回调;3配送回调
	 */
	public static void back(HttpServletRequest request, HttpServletResponse response, int type) {
		log.info("进入" + Config.value + ",回调处理接口....");
		try {
			String xml = "";
			try {
				BufferedReader re = request.getReader();
				String org = "";
				while ((org = re.readLine()) != null)
					xml = xml + org;
			} catch (Exception e) {
				log.error(Config.value + ",回调请求参数<getReader>异常:" + e);
			}

			if (!StringUtil.isNulls(xml))
				log.info(Config.value + "回调处理接口请求xml<getReader>:" + xml);

			JSONObject param = PrivateTool.paramtenToJson(request.getParameterMap());
			if (null != param && !param.isEmpty()) {
				log.info(Config.value + ",回调处理接口请求参数:" + param);
			}

			BaseController.setResponseText(response, back(param, xml, type));
		} catch (Exception e) {
			log.error(Config.value + ",回调处理接口实现异常:" + e);
			BaseController.setResponseText(response, "回调处理接口实现异常:" + e.getMessage());
		}
	}

	/**
	 * xml报文
	 * 
	 * @param request
	 * @return
	 */
	public static String getXML(HttpServletRequest request) {
		String xml = "";
		try {
			BufferedReader re = request.getReader();
			String org = "";
			while ((org = re.readLine()) != null)
				xml = xml + org;
		} catch (Exception e) {
			log.error(Config.value + ",回调请求参数<getReader>异常:" + e);
		}
		return xml;
	}

	/**
	 * 回调接口处理
	 * 
	 * @param param
	 * @param type
	 * @return
	 */
	public static String back(JSONObject param, String xml, int type) {
		if (0 == type) {
			log.info("统一回调接口....");
			return "通知后台处理该接口";
		} else if (1 == type) {////////////////////// ?
			log.info("支付回调接口....");
//			BackYeGaunDeal.payBack(xml);
			return tpBack();
		} else if (2 == type) {////////////////////// ?
			log.info("承保回调接口...");
//			BackYeGaunDeal.contractBack(xml);
			return tpBack();
		} else if (3 == type) {
			log.info("配送回调接口...");
			return "通知后台处理该接口";
		} else {
			log.info("其他回调接口请求.........");
			return "通知后台处理该接口";
		}
	}

	/**
	 * 支付 默认回调返回报文
	 * 
	 * @return
	 */
	private static String tpBack() {
		String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		request = request + "\n" + "<request>";
		request = request + "\n" + "<head>";

		request = request + "\n\t" + "<status>" + "S" + "</status>";
		request = request + "\n\t" + "<errCode>" + "0000" + "</errCode>";
		request = request + "\n\t" + "<errMsg>" + "0000" + "</errMsg>";

		request = request + "\n" + "</head>";
		request = request + "\n" + "<body>";

		request = request + "\n" + "</body>";
		request = request + "\n" + "</request>";

		return request;
	}
}
